'use strict';
let that = this
//判断当前浏览器版本使用什么请求
function httpTypeByVerSion(e, n) {
    if (e > n) {
        return 'fetch'
    } else {
        return 'xhr'
    }
}
//获取当前浏览器
function BrowserType() {
    let obj = {
        name: '',
        version: ''
    }
    let userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    console.log(navigator.userAgent)
    let isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器  
    let isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera || userAgent.indexOf("rv:11") > -1; //判断是否IE浏览器  
    let isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器   
    let isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器  
    let isSafari = userAgent.indexOf("Safari") > -1; //判断Safari浏览器  
    if (isIE) {
        if (userAgent.indexOf("rv:11") > -1) {
            obj.name = "IE11"
            obj.version = 11
            return obj;
        }
        let reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        let fIEVersion = parseFloat(RegExp["$1"]);
        let versionArr = [7, 8, 9, 10, 11, 12]
        if (versionArr.includes(fIEVersion)) {
            obj.name = 'IE' + fIEVersion
            obj.version = fIEVersion
            return
        }
        if (fIEVersion == '0') {
            return "0"
        } //IE版本过低  
    } //isIE end  
    if (isFF) {
        obj.name = 'Firefox'
        obj.version = getFFVer('Firefox')
        return obj
    }
    if (isSafari) {
        obj.name = 'Safari'
        obj.version = getFFVer('Safari')
        return obj
    }
    if (isChrome) {
        obj.name = 'Chrome'
        obj.version = getFFVer('Chrome')
        return obj
    }
}

//获取谷歌、火狐版本
function getFFVer(e) {
    let ua = navigator.userAgent;
    let index = e.length + 1
    let b = ua.indexOf(e + '/');
    if (b < 0) {
        return 0;
    }
    console.log(parseFloat(ua.substring(b + index, ua.lastIndexOf("\."))))
    return parseFloat(ua.substring(b + index, ua.lastIndexOf("\.")));
}
//判断当前环境是node还是windows
export function getCurrentEnvironment() {
    let type
    if (typeof XMLHttpRequest !== 'undefined') {
        let {
            name,
            version
        } = BrowserType()
        switch (name) {
            case 'Chrome':
                type = httpTypeByVerSion(version, 42)
                break;
            case 'Safari':
                type = httpTypeByVerSion(version, 42)
                break;
            case 'Firefox':
                type = httpTypeByVerSion(version, 39)
                break;
            case name.indexOf('IE'):
                type = 'xhr'
                break;
            default:
                type = 'xhr'
                break;
        }
    } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
        type = 'http'
    }
    return type
}